package org.agpsoft.simpledatareceiver;

import java.io.IOException;
import java.io.OutputStream;

import org.agpsoft.datareceiverinfrastructure.common.IDecodedData;
import org.agpsoft.datareceiverinfrastructure.common.RawData;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverCloseException;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverOpenException;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverRecordException;
import org.agpsoft.datareceiverinfrastructure.recorder.IDecodedDataRecorder;

public class SimpleDecodedDataRecorder implements IDecodedDataRecorder {

	private OutputStream outputStream = null;
	
	@Override
	public void recordData(IDecodedData decodedData)
			throws DataReceiverRecordException {
		RawData rawData = new RawData();
		decodedData.getRawPresentation( rawData );
		try {
			outputStream.write( rawData.getData() );
		} catch (IOException e) {
			throw new DataReceiverRecordException();
		}
	}

	@Override
	public void close() throws DataReceiverCloseException {
	}

	@Override
	public void open(Object context) throws DataReceiverOpenException {
		SimpleDataReceiverContext basicDataReceiverContext = 
			(SimpleDataReceiverContext) context;
		outputStream = basicDataReceiverContext.getDecodedRecordStream();
	}

}
